package com.neusoft.nep.SystemAdminstrator.entity;

import com.neusoft.nep.entity.Feedback;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class SystemFeedback extends Feedback implements Serializable {
    private LocalDate date;     // 日期部分
    private LocalTime timePart;     // 时间部分
    private Boolean assigned = false;
    private static final DateTimeFormatter FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public Boolean getAssigned() {
        return assigned;
    }

    public void setAssigned(Boolean assigned) {
        this.assigned = assigned;
    }
// 无参构造函数
//    public SystemFeedback() {
//        super();
//    }

    // 从现有Feedback对象创建
    public SystemFeedback(Feedback baseFeedback) {
        this.setIndex(baseFeedback.getIndex());
        this.setTime(baseFeedback.getTime());
        this.setName(baseFeedback.getName());
        this.setPhoneNumber(baseFeedback.getPhoneNumber());
        this.setSex(baseFeedback.getSex());
        this.setBirthday(baseFeedback.getBirthday());
        this.setProvince(baseFeedback.getProvince());
        this.setCity(baseFeedback.getCity());
        this.setAddress(baseFeedback.getAddress());
        this.setAqiLevel(baseFeedback.getAqiLevel());
        this.setDescription(baseFeedback.getDescription());
    }

    // 本地直接创建（使用日期时间对象）
    public SystemFeedback(int index,String province,String name,String sex,String phonenuber,String birthday, String city, String address,
                            String aqiLevel, String description, LocalDate date, LocalTime time) {
        super();
        this.setIndex(index);
        this.setProvince(province);
        this.setName(name);
        this.setSex(sex);
        this.setPhoneNumber(phonenuber);
        this.setBirthday(birthday);
        this.setCity(city);
        this.setAddress(address);
        this.setAqiLevel(aqiLevel);
        this.setDescription(description);
        this.setDate(date);
        this.setTimePart(time);
    }

    // 本地直接创建（使用日期时间字符串）
    public SystemFeedback(int index,String name,String sex,String phonenumber,String birthday, String province, String city, String address,
                            String aqiLevel, String description, String dateTimeStr) {
        super();
        this.setIndex(index);
        this.setName(name);
        this.setSex(sex);
        this.setPhoneNumber(phonenumber);
        this.setBirthday(birthday);
        this.setProvince(province);
        this.setCity(city);
        this.setAddress(address);
        this.setAqiLevel(aqiLevel);
        this.setDescription(description);
        this.setTime(dateTimeStr); // 自动解析日期时间
    }

    // 重写父类的setTime方法
    @Override
    public void setTime(String timeStr) {
        super.setTime(timeStr);
        parseDateTime(timeStr);
    }

    // 获取组合后的完整时间字符串
    @Override
    public String getTime() {
        return super.getTime();
    }

    // 获取日期部分
    public LocalDate getDate() {
        return date;
    }

    // 设置日期部分
    public void setDate(LocalDate date) {
        this.date = date;
        super.setTime(formatDateTime(date, this.timePart));
    }

    // 获取时间部分
    public LocalTime getTimePart() {
        return timePart;
    }

    // 设置时间部分
    public void setTimePart(LocalTime time) {
        this.timePart = time;
        super.setTime(formatDateTime(this.date, time));
    }

    // 解析日期时间字符串
    private void parseDateTime(String timeStr) {
        if (timeStr == null || timeStr.isEmpty()) {
            this.date = null;
            this.timePart = null;
            return;
        }

        try {
            java.time.LocalDateTime dateTime = java.time.LocalDateTime.parse(timeStr, FORMATTER);
            this.date = dateTime.toLocalDate();
            this.timePart = dateTime.toLocalTime();
        } catch (DateTimeParseException e) {
            System.err.println("日期时间格式错误: " + timeStr);
            this.date = null;
            this.timePart = null;
        }
    }

    // 格式化日期时间
    private static String formatDateTime(LocalDate date, LocalTime time) {
        if (date == null || time == null) {
            return null;
        }
        return date.atTime(time).format(FORMATTER);
    }
}